package main

import (
	"flag"
	"fmt"
	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/core/service"
	"github.com/zeromicro/go-zero/zrpc"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
	"t-go-zero/service/user/rpc/internal/config"
	"t-go-zero/service/user/rpc/internal/server"
	"t-go-zero/service/user/rpc/internal/svc"
	"t-go-zero/service/user/rpc/userclient"
)

var configFile = flag.String("f", "etc/user.yaml", "the config file")

func main() {
	// 获取命令行参数
	flag.Parse()

	// 解析配置文件
	var c config.Config
	conf.MustLoad(*configFile, &c)

	// 创建中间服务
	ctx := svc.NewServiceContext(c)

	// 注册rpc服务  也是可以鉴权的    http的阉割版
	s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
		userclient.RegisterUserServer(grpcServer, server.NewUserServer(ctx))

		if c.Mode == service.DevMode || c.Mode == service.TestMode {
			reflection.Register(grpcServer)
		}
	})

	// 关闭RPC服务
	defer s.Stop()

	fmt.Println(c.Name, c.ListenOn)

	// 启动Rpc服务
	s.Start()
}
